with ada.text_io; use ada.text_io;
package body c_arbre is

  
  procedure abuit(p: out arbre) is
  begin
  for i in index'first..(index'last - 1) loop
    m(i).s:= i + 1;
  end loop;

  m(index'last).s:= 1;
  lliure:= 1;
  --  p:= 0;
  end abuit;

  procedure posa(r: in out arbre; e: character) is
  i: index:= lliure;
  begin
    m(i).i:= i;
    m(i).val:= e;
    lliure:= m(i).s;
    r:= m(i);
  end posa;
  
--procedure posa(r: in out node; e: element);
--  procedure posa_fd(r: in out arbre; e: character) is
--  begin
--    r.fd:= e;
--  end posa_fd;
--
--  procedure posa_fe(r: in out arbre; e: character) is 
--  i: index:= lliure;
--  begin
--    m(i).i:= i;
--    r.fe:= i;
--    m(i).val:= e;
--    lliure:= m(i).s;
--  end posa_fe;


  function consulta(i: in index) return character is
  begin
	return m(i).val;
  end consulta;

  function fe(r: in arbre) return index is
  begin
    return r.fe;
  end fe;

  function fd(r: in arbre) return index is
  begin
    return r.fd;
  end fd;

  procedure posa_elem(p: in arbre; ne: character) is
  i: index:= lliure;
  begin
    m(lliure).val:= ne;
    lliure:= m(lliure).s;
  end posa_elem;

  procedure posa_op(p: in arbre; ne: character) is
  i: index:= lliure;
  begin
    m(lliure).val:= ne;
    lliure:= m(lliure).s;
  end posa_op;

--  procedure elimina(p: in out node) is
--  begin
--    if ((p.fe = 0) and (p.fd = 0)) then
--      --p.e := 0;
--      null;
--    end if;
--  end elimina;

  function empelta(op: in character; fe: in arbre; fd: in arbre) return arbre is
  i: index:= lliure;
  begin
    m(i).i:= i;
    m(i).val:= op;
    lliure:= m(i).s;
	m(i).fe:= fe.i;
	m(i).fd:= fd.i;
    return m(i);
  end empelta;

procedure print_index(p: in index) is
  package INT_IO is new INTEGER_IO(index); use INT_IO;
begin
put(p);
end print_index;

procedure print(p: in arbre) is
  package INT_IO is new INTEGER_IO(index); use INT_IO;
begin
put("Index:");
put(p.i);
new_line(1);
put("Valor:");
put(p.val);
new_line(1);
put("Fe");
put(p.fe);
new_line(1);
put("Fd");
put(p.fd);
new_line(1);
end print;

procedure get_inordre(r: in out arbre; p:in out pila) is
x: arbre:= r;
begin
  if (fe(r) /= 0) then 
  	x:= m(fe(r));
  	get_inordre(x, p);
  end if;
  empila(p, r.val);

  if (fd(r) /= 0) then 
  	x:= m(fd(r));
  	get_inordre(x, p);
  end if;
end get_inordre;

  procedure sig_inordre(p: in out pila; i: out character) is
  begin
  	i:= cim(p);
	put(i);
	desempila(p);
  end sig_inordre;


procedure list_all is
i: integer:= 0;
begin
put("---------");
for i in index'first..(index'first + 10) loop
print(m(i));
end loop;
put("---------");
new_line(1);

end list_all;

begin

  for i in index'first..(index'last - 1) loop
    m(i).s:= i + 1;
  end loop;

  m(index'last).s:= 1;
  lliure:= 1;

end c_arbre;
